<?php
/**
 * Created by PhpStorm.
 * User: lyq
 * Date: 2018/12/01
 * Time: 上午10:00
 */

namespace app\common\model;

use think\Db;


class Category extends Common
{

    /***
     * 根据parent_id 获取分类 0 时获取一级分类  其他根据值获取下级分类
     **/
    static public function getCategory($parent_id = 0)
    {
        $list = Db::name('category')
            ->where('parent_id', $parent_id)
            ->where('is_visible', 1)
            ->where('is_valid', 1)
            ->order('display_order DESC,id DESC')
            ->field('title, id, img_url')
            ->select();
        return empty($list) ? [] : $list;
    }

    /**
     * 获取首页推荐分类
     */
    public static function getIndexCate()
    {
        $model = new self();
        $list = $model->where("is_visible", 1)->where("is_valid", 1)->where("is_index", 1)->order("display_order desc, id desc")->field("title, id, img_url")->select();
        return empty($list) ? [] : $list;
    }

    /**
     * 根据分类获取品牌
     */
    static public function getBrand($category_id)
    {

        $brand_id = \db("category_brand")->where("category_id1", $category_id)->column("brand_id");
        $list = \db("brand")->whereIn("id", $brand_id)->field("title, id")->select();
        return empty($list) ? [] : $list;
    }
}
